import 'dart:async';

import 'package:df/page/login/login.dart';
import 'package:df/page/login/login_page.dart';
import 'package:df/page/login/login_response.dart';
import 'package:df/page/register/register.dart';
import 'package:df/page/register/register_response.dart';
import 'package:df/page/router/router.dart';
import 'package:df/page/sms/sms_page.dart';
import 'package:df/page/sms/sms_response.dart';
import 'package:df/page/sms/sms.dart';
import 'package:df/service/api_service.dart';
import 'package:df/utils/sp_key.dart';
import 'package:df/widgets/dialog_loading.dart';
import 'package:df/widgets/toast_utils.dart';
import 'package:df/utils/logger_utils.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:df/generated/l10n.dart';

///注册
class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}


late LoadingDialog _loadingDialog;
class _RegisterPageState extends State<RegisterPage> {
  TextEditingController _phoneController = TextEditingController();
  TextEditingController _codeController = TextEditingController();


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(S.current.registerUser),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            GoRouter.of(context).pop();
          },
        ),
      ),

      body: SafeArea(
        top: true,
        bottom: false,
        left: false,
        right: false,

        child: Padding(
          padding: const EdgeInsets.all(16.0),

          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [



          Row(children: [
          Expanded(
            child: TextField(
              controller: _phoneController,
              keyboardType: TextInputType.phone,
              decoration: InputDecoration(
                labelText: S.current.enter11DigitAccount,
              ),
            ),
          ),
          ],),




              SizedBox(height: 16.0),




              Row(children: [
                  Expanded(
                    child: TextField(
                      controller: _codeController,
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        labelText: S.current.password,
                      ),
                    ),
                  ),
              ],),



              SizedBox(height: 32.0),



              ElevatedButton(
                onPressed: () {
                  String phoneNum = _phoneController.text;
                  String code = _codeController.text;

                  if(SmsPage.isValidPhoneNumber(phoneNum) && code.isNotEmpty) {
                    LogUtils.w('【手机号】 ${phoneNum}');
                    LogUtils.w('【验证码】 ${code}');
                    // 创建 Sms 实例
                    Register register = Register(
                        account: phoneNum,
                        mobilePhone: phoneNum,
                        userName: phoneNum,
                        pwd: code,
                    );

                    doRegister(register, context);
                  } else {
                    ToastUtils.show(S.current.fillContentInvalid);
                  }

                },
                child: Text(S.current.register),
              ),
            ],
          ),


        ),
      ),
    );
  }


  static void doRegister(Register register, BuildContext context) {
    _loadingDialog = new LoadingDialog(
      text: S.of(context).pleaseWait,
    );
    showDialog<Null>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return _loadingDialog;
        });

    Future<RegisterResponse> registerResponse = ApiService.postRegister(register);
    registerResponse.then((response) {
      LogUtils.w("【登录】结果= ${response.resultMsg}");
      int? code = response.resultCode;
      LogUtils.w("【登录】结果= ${code}");
      if(code == ApiService.kCodeSuccess) {
        LogUtils.w("【登录成功】注册成功= ${response}");
        ToastUtils.show(S.current.registrationSuccessful);
        GoRouter.of(context).pop();
      } else {
        ToastUtils.show('${S.current.registrationFailed}:\n${response.resultMsg}');
      }

      _loadingDialog.pop();
    });
  }

}
